/*
  Copyright 2012 Sergey Ostanin

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

package ru.irenproject;

import javax.inject.Inject;

import ru.irenproject.store.AbstractThing;
import ru.irenproject.store.Event;
import ru.irenproject.store.Stash;
import ru.irenproject.store.Thing;

import com.google.protobuf.Message;

public final class TestEditor extends AbstractThing implements Visual {
  public static final class SetViewModeInput {
    public boolean viewMode;
  }

  public static TestEditor create(Test test) {
    TestEditor res = test.store().create(TestEditor.class);
    res.prepare(test);
    return res;
  }

  @Stash private UiProtos.TestEditor.Builder $;
  private final QuestionEditorProvider fQuestionEditorProvider;

  @Inject TestEditor(QuestionEditorProvider questionEditorProvider) {
    fQuestionEditorProvider = questionEditorProvider;
  }

  private void prepare(Test test) {
    $.setViewMode(false)
     .setQuestionViewer(store().create(QuestionViewer.class).id())
     .setSectionTreeEditor(SectionTreeEditor.create(test.sectionTree()).id());
    sectionTreeEditor().addListener(this);
    questionViewer().setSectionPathSupplier(sectionTreeEditor());

    displayQuestionList();
    displayQuestion();
  }

  private void setQuestionEditor(QuestionEditor editor) {
    if (editor == null) {
      $.clearQuestionEditor();
    } else {
      $.setQuestionEditor(editor.id());
    }
  }

  private SectionTreeEditor sectionTreeEditor() {
    return get($.getSectionTreeEditor(), SectionTreeEditor.class);
  }

  private QuestionListEditor questionListEditor() {
    return get($.getQuestionListEditor(), QuestionListEditor.class);
  }

  private QuestionViewer questionViewer() {
    return get($.getQuestionViewer(), QuestionViewer.class);
  }

  @Override public Message render() {
    return $.build();
  }

  private void displayQuestionList() {
    QuestionListEditor e = QuestionListEditor.open(sectionTreeEditor().selectedSection().questionList());
    e.addListener(this);
    $.setQuestionListEditor(e.id());
  }

  private void displayQuestion() {
    QuestionItem item = questionListEditor().activeItem();
    if ($.getViewMode()) {
      setQuestionEditor(null);
      questionViewer().setQuestionItem(item);
    } else {
      setQuestionEditor(item == null ? null : fQuestionEditorProvider.open(item.question()));
      questionViewer().setQuestionItem(null);
    }
  }

  @Override protected void handleEvent(Event e, Thing source) {
    if (e instanceof SectionTreeEditor.SelectSectionEvent) {
      displayQuestionList();
      displayQuestion();
    } else if (e instanceof QuestionListEditor.SelectItemEvent) {
      displayQuestion();
    }
  }

  @Action(markAsModified = false)
  public void doSetViewMode(SetViewModeInput in) {
    if ($.getViewMode() != in.viewMode) {
      $.setViewMode(in.viewMode);
      displayQuestion();
    }
  }

  public void setProfileSupplier(ProfileSupplier supplier) {
    questionViewer().setProfileSupplier(supplier);
  }
}
